package com.liao.healthydiet.ui.adapter;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.liao.healthydiet.R;
import com.liao.healthydiet.domain.Food;

public class ListViewFoodLayoutAdapter extends BaseAdapter {

    private List<Food> list;

    private Context context;
    private LayoutInflater layoutInflater;

    public ListViewFoodLayoutAdapter(Context context,ArrayList<Food>list) {
        this.context = context;
        this.list = list;
        this.layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Food getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("InflateParams")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.listview_food_layout, null);
            convertView.setTag(new ViewHolder(convertView));
        }
        initializeViews((Food) getItem(position), (ViewHolder) convertView.getTag());
        return convertView;
    }

    private void initializeViews(Food food, ViewHolder holder) {
        holder.imgFoodImg.setImageResource(food.getImage());
        holder.tvFoodName.setText(food.getName());
        holder.tvFoodCollocation.setText(food.getCollocation());
    }

    protected static class ViewHolder {
        private ImageView imgFoodImg;
        private TextView tvFoodName;
        private TextView tvFoodCollocation;

        ViewHolder(View view) {
            imgFoodImg = (ImageView) view.findViewById(R.id.img_food_img);
            tvFoodName = (TextView) view.findViewById(R.id.tv_food_name);
            tvFoodCollocation = (TextView) view.findViewById(R.id.tv_food_collocation);
        }
    }
}
